package com.iims.product;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.product.domain.ProductType;
import com.iims.product.service.IProductTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:33
 */
@Api(tags = "G-产品类型管理")
@RestController
@RequestMapping("/business/product/type")
public class ProductTypeController extends BaseController {
    @Autowired
    private IProductTypeService proTypeService;

    /**
     * 获取产品类型列表
     */
    @ApiOperation("分页查询产品类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "产品类型名称", name = "proTypeName", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:productType:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore ProductType productType) throws Exception {
        startPage();
        List<ProductType> list = proTypeService.selectProductTypeList(productType);
        return getDataTable(list);
    }

    /**
     * 根据产品类型编号获取详细信息
     */
    @Deprecated
    //@ApiOperation("根据产品类型编号获取详细信息")
    //@ApiImplicitParam(name = "productTypeId", value = "产品类型Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:productType:query')")
    @GetMapping(value = "/{productTypeId}")
    public AjaxResult getInfo(@PathVariable Long productTypeId) {
        return AjaxResult.success(proTypeService.selectOne(productTypeId));
    }

    /**
     * 新增产品类型
     */
    @ApiOperation("新增产品类型")
    @ApiOperationSupport(ignoreParameters = {"prodTypeId"})
    @PreAuthorize("@ss.hasPermi('business:productType:add')")
    @Log(title = "产品类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProductType productType) {
        if (UserConstants.NOT_UNIQUE.equals(proTypeService.checkProductTypeNameUnique(productType))) {
            return AjaxResult.error("新增产品类型'" + productType.getProdTypeName() + "'失败，产品类型名称已存在");
        }
        return toAjax(proTypeService.insertProductType(productType));
    }

    /**
     * 修改产品类型
     */
    @ApiOperation("修改产品类型")
    @PreAuthorize("@ss.hasPermi('business:productType:edit')")
    @Log(title = "产品类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ProductType productType) {
        if (UserConstants.NOT_UNIQUE.equals(proTypeService.checkProductTypeNameUnique(productType))) {
            return AjaxResult.error("修改产品类型'" + productType.getProdTypeName() + "'失败，产品类型名称已存在");
        }
        return toAjax(proTypeService.updateProductType(productType));
    }

    /**
     * 删除产品类型
     */
    @ApiOperation("删除产品类型")
    @PreAuthorize("@ss.hasPermi('business:productType:remove')")
    @Log(title = "产品类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{proTypeId}")
    public AjaxResult remove(@ApiParam("产品类型Id") @PathVariable Long proTypeId) {
        if (proTypeService.checkProductTypeUse(proTypeId)) {
            return AjaxResult.error("删除产品类型失败，产品类型已使用！请先删除关联产品类型的产品！");
        }
        return toAjax(proTypeService.deleteProductTypeById(proTypeId));
    }
}
